﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace BaseTypesControlLib.Controls
{
    public enum DateFieldMode { Date, DateTime, Time }
    public enum TimeFormat { Short, Long, Default }
    public enum DateFormat { Short, Medium, Long, Full, Default }

    public class DateFieldControl:LabelAndSelectionPanel
    {

        static DateFieldControl()
        {
            FrameworkPropertyMetadata fpm = new FrameworkPropertyMetadata(DateTime.Now,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure, OnPropertyChanged);
            DateProperty = DependencyProperty.Register("Date", typeof(DateTime), 
                typeof(DateFieldControl), fpm);

            fpm = new FrameworkPropertyMetadata(DateFieldMode.DateTime,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure, OnPropertyChanged);
            ModeProperty = DependencyProperty.Register("Mode", typeof(DateFieldMode),
                typeof(DateFieldControl), fpm);

            fpm = new FrameworkPropertyMetadata(DateFormat.Default,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure, OnPropertyChanged);
            DateFormatProperty = DependencyProperty.Register("DateFormat", typeof(DateFormat),
                typeof(DateFieldControl), fpm);

            fpm = new FrameworkPropertyMetadata(TimeFormat.Default,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure, OnPropertyChanged);
            TimeFormatProperty = DependencyProperty.Register("TimeFormat", typeof(TimeFormat),
                typeof(DateFieldControl), fpm);
        }



        public DateTime Date
        {
            get { return (DateTime)GetValue(DateProperty); }
            set { SetValue(DateProperty, value); }
        }
        public static readonly DependencyProperty DateProperty;

        public DateFieldMode Mode
        {
            get { return (DateFieldMode)GetValue(ModeProperty); }
            set { SetValue(ModeProperty, value); }
        }
        public static readonly DependencyProperty ModeProperty;

        public DateFormat DateFormat
        {
            get { return (DateFormat)GetValue(DateFormatProperty); }
            set { SetValue(DateFormatProperty, value); }
        }
        public static readonly DependencyProperty DateFormatProperty ;

        public TimeFormat TimeFormat
        {
            get { return (TimeFormat)GetValue(TimeFormatProperty); }
            set { SetValue(TimeFormatProperty, value); }
        }
        public static readonly DependencyProperty TimeFormatProperty;

        

        private static void OnPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            DateFieldControl ctrl = o as DateFieldControl;
            if (ctrl != null)
                ctrl.UpdateText();
        }

        private void UpdateText()
        {
            txtText.Text = Date.ToString(GetDateFormatString());
        }

        private string GetDateFormatString()
        {
            string res = "";
            if (Mode == DateFieldMode.Date || Mode == DateFieldMode.DateTime)
                res += DateFormatToString(DateFormat);
            if (Mode == DateFieldMode.Time || Mode == DateFieldMode.DateTime)
                res += (" "+TimeFormatToString(TimeFormat));
            return res;
        }
        private string DateFormatToString(DateFormat format)
        {
            switch (format)
            {
                case Controls.DateFormat.Short:
                    return "d/M/yyyy";
                case Controls.DateFormat.Medium:
                    return "d MMM yyyy";
                case Controls.DateFormat.Long:
                    return "ddd, d MMM yyyy";
                case Controls.DateFormat.Full:
                    return "dddd, d MMMM yyyy";
                default:
                    return "d MMM yyyy";
            }
        }
        private string TimeFormatToString(TimeFormat format)
        {
            switch (format)
            {
                case Controls.TimeFormat.Short:
                    return "HH:mm";
                case Controls.TimeFormat.Long:
                    return "HH:mm:ss";
                default:
                    return "HH:mm";
            }
        }
    }
}
